package com.yanqu.road.dao.impl.cross.unionwar;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.union.UnionBaseInfo;
import com.yanqu.road.entity.unionwar.UnionWarChampionUnionInfo;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossUnionWarChampionDaoImpl extends TempDataObjectDao<UnionWarChampionUnionInfo> {
    @Override
    protected boolean addData(UnionWarChampionUnionInfo data) {
        PreparedStatement call = null;
        String sql = "insert into t_c_unionwar_champion(timeMark,activityId,serverIdList,unionBaseInfo,unionUid,groupId) values(?,?,?,?,?,?)";
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setInt(i++, data.getTimeMark());
            call.setInt(i++, data.getActivityId());
            call.setString(i++, data.getServerIdList());
            call.setString(i++, JSON.toJSONString(data.getUnionBaseInfo()));
            call.setString(i++, data.getUnionUid());
            call.setInt(i++, data.getGroupId());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected boolean updateData(UnionWarChampionUnionInfo data) {
        return false;
    }

    @Override
    protected UnionWarChampionUnionInfo parseData(ResultSet rs) throws SQLException {
        UnionWarChampionUnionInfo data = new UnionWarChampionUnionInfo();
        data.setTimeMark(rs.getInt("timeMark"));
        data.setActivityId(rs.getInt("activityId"));
        data.setServerIdList(rs.getString("serverIdList"));
        data.setGroupId(rs.getInt("groupId"));
        String baseUnionInfoStr = rs.getString("unionBaseInfo");
        if (!StringUtils.isNullOrEmpty(baseUnionInfoStr)) {
            UnionBaseInfo unionBaseInfo = JSON.parseObject(baseUnionInfoStr, new TypeReference<UnionBaseInfo>() {});
            if (unionBaseInfo != null) {
                data.setUnionBaseInfo(unionBaseInfo);
            }
        }
        data.setUnionUid(rs.getString("unionUid"));
        return data;
    }

    public Map<Integer, Map<Integer, List<UnionWarChampionUnionInfo>>> getUnionWarChampionMap() {
        String sql = "select * from t_c_unionwar_champion";
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, Map<Integer, List<UnionWarChampionUnionInfo>>> dataMap = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(sql);
            rs = call.executeQuery();
            while (rs.next()) {
                UnionWarChampionUnionInfo data = parse(rs);
                if (!dataMap.containsKey(data.getTimeMark())) {
                    dataMap.put(data.getTimeMark(), new ConcurrentHashMap<>());
                }
                if (!dataMap.get(data.getTimeMark()).containsKey(data.getActivityId())) {
                    dataMap.get(data.getTimeMark()).put(data.getActivityId(), new ArrayList<>());
                }
                dataMap.get(data.getTimeMark()).get(data.getActivityId()).add(data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }
}
